package me.zhengjie.tools.service.dto

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.bean.copier.CopyOptions
import com.fasterxml.jackson.annotation.JsonTypeName
import me.zhengjie.common.annotation.Query
import me.zhengjie.common.utils.JacksonUtil
import org.apache.commons.lang3.builder.ToStringBuilder

import java.sql.Timestamp

/**
 * @author Kuki Wu
 * @date 2019-09-05
 */
@JsonTypeName("LocalStorageQueryCriteria")
data class LocalStorageQueryCriteria (
    // 模糊
    @Query(blurry = "name,suffix,type,operate,size")
    var blurry: String? = null,

    @Query(type = Query.Type.GREATER_THAN, propName = "createTime")
    var startTime: Timestamp? = null,

    @Query(type = Query.Type.LESS_THAN, propName = "createTime")
    var endTime: Timestamp? = null,
){
    annotation class Update
    companion object {
        override fun toString(): String {
            val builder = ToStringBuilder(this)
            val fields = this.javaClass.declaredFields
            try {
                for (f in fields) {
                    f.isAccessible = true
                    builder.append(f.name, f[this]).append("\n")
                }
            } catch (e: Exception) {
                builder.append("toString builder encounter an error")
            }
            return builder.toString()
        }

        fun tojson() {
            JacksonUtil.toJSONString(this)
        }

    }

    fun copy(source: LocalStorageQueryCriteria?) {
        BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true))
    }
}